package com.kfm.wiki.handler;

import com.kfm.wiki.ex.BusinessException;
import com.kfm.wiki.util.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ControllerExceptionHandler {

    private Logger LOG = LoggerFactory.getLogger(ControllerExceptionHandler.class);


    @ResponseBody
    @ExceptionHandler(BindException.class)
    public R exceptionHandler(BindException ex){
        LOG.error("参数绑定错误 {}", ex.getLocalizedMessage());
        return R.fail(ex.getAllErrors().get(0).getDefaultMessage());
    }

    @ResponseBody
    @ExceptionHandler(BusinessException.class)
    public R businessExceptionHandler(BusinessException ex){
        LOG.error("业务异常 {}", ex.getLocalizedMessage());
        return R.fail(ex.getMessage());
    }
}
